<template>
  <div class="login-container">
    <el-form :model="ruleForm2" :rules="rules2"
             status-icon
             ref="ruleForm2"
             label-position="left"
             label-width="0px"
             class="demo-ruleForm login-page">
      <h3 class="title">教师登录</h3>
      <el-form-item prop="username">
        <el-input type="text"
                  v-model="ruleForm2.username"
                  auto-complete="off"
                  placeholder="用户名"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password"
                  v-model="ruleForm2.password"
                  auto-complete="off"
                  placeholder="密码"
        ></el-input>
      </el-form-item>
      <el-checkbox
        v-model="checked"
        class="rememberMe"
      >记住密码
      </el-checkbox>
      <el-form-item style="width:100%;">
        <el-button type="primary" style="width:100%;" @click="handleSubmit" :loading="logining">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import {getLogin} from "../../api/data";

export default {
  data() {
    return {
      logining: false,
      ruleForm2: {
        roles: '',
        username: '',
        password: '',
      },
      rules2: {
        username: [{required: true, message: '请输入账号', trigger: 'blur'}],
        password: [{required: true, message: '请输入密码', trigger: 'blur'}]
      },
      checked: false
    }
  },
  methods: {
    handleSubmit() {
      let data = {rid: 2, pwd: this.ruleForm2.password, uid: this.ruleForm2.username}
      getLogin(JSON.stringify(data)).then(res => {
        sessionStorage.setItem('token',JSON.stringify(res.data))
        this.$refs.ruleForm2.validate((valid) => {
          if (valid) {
            this.logining = true;
            if (res.code === 0) {
              this.logining = false;
              sessionStorage.setItem('user', this.ruleForm2.username);
              this.$router.push({path: '/home'});
            } else if (res.code === 1) {
              this.logining = false;
              this.$message.error('账号或者密码错误!');
            } else if (res.code === 2) {
              this.logining = false;
              this.$message.error('请检查信息是否输入完整');
            } else {
              this.logining = false;
              this.$message.error('权限不足');
            }
          } else {
            console.log('提交错误');
            return false;
          }
        })
      })
    }
  }
};
</script>

<style scoped>
.login-container {
  width: 100%;
  height: 100%;
}

.title {
  margin-top: auto;
  text-align: center;
}

.login-page {
  -webkit-border-radius: 5px;
  border-radius: 5px;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

label.el-checkbox.rememberMe {
  margin: 0px 0px 15px;
  text-align: left;
}
</style>
